#include <math.h>
#include "Algorithm.h"
#include "stdlib.h"

//白细胞
int Leukocytes(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (V > 180) vid = 2;
	if (V < 210) vid = 2;
	if (V < 165) vid = 3;
	if (V < 145) vid = 4;

	if (H < 30) hid = 1;

	id = vid;
	id = hid > 0 ? hid : id;

	return id;
}

//亚硝酸盐
int Nitrite(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (S*255 < 15) vid = 1;
	if (S*255 >= 15) vid = 2;

	id = vid;

	return id;
}

//尿胆原
int UrinaryBladder(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 20) hid = 1;
	if (H < 10) hid = 2;
	if (abs(G - B) < 5) hid = 3;
	if (B > G)  hid = 4;

	id = hid;

	return id;
}

//蛋白质
int Protein(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 220) hid = 5;
	if (H < 150) hid = 4;
	if (H < 115) hid = 3;
	if (H < 85)  hid = 2;
	if (H < 70)  hid = 1;


	id = hid;

	return id;
}

//酸碱度
int PH(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 100) hid = 6;
	if (H < 90)  hid = 5;
	if (H < 80)  hid = 4;
	if (H < 60)  hid = 3;
	if (H < 55)  hid = 2;
	if (H < 45)  hid = 1;

	id = hid;

	return id;
}

//潜血
int OccultBlood(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 100) hid = 4;
	if (H < 70)  hid = 3;
	if (H < 50)  hid = 2;
	if (H < 40)  hid = 1;

	id = hid;

	return id;
}

//比重
int Proportion(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 100) hid = 1;
	if (H < 80)  hid = 2;
	if (H < 60)  hid = 3;
	if (H < 50)  hid = 4;
	//if (H < 40)  hid = 3;
	//if (H < 30)  hid = 2;

	if (V > 130 && V < 170) vid = 5;

	id = vid > hid ? vid : hid;

	return id;
}

//酮体
int Ketone(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (S * 255 < V) sid = 1;
	if (abs((int)(S * 255 - V)) < 15) sid = 2;
	if (S * 255 > V) sid = 3;



	id = sid;

	return id;
}

//胆红素
int Bilirubin(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 360) hid = 1;
	if (H < 320) hid = 2;
	if (H < 300) hid = 3;

	id = hid;

	return id;
}

//葡萄糖
int Glucose(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 150) hid = 1;
	if (H < 95)  hid = 2;
	if (H < 50)  hid = 3;

	id = hid;

	return id;
}

//抗坏血酸
int AscorbicAcid(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 175) hid = 1;
	if (H < 135) hid = 2;
	if (H < 95)  hid = 3;

	id = hid;

	return id;
}

//微量白蛋白
int MicroAlbumin(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	return id;
}

//肌酐
int Creatinine(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	if (H < 120) hid = 4;
	if (H < 90)  hid = 3;
	if (H < 80)  hid = 2;
	if (H < 70)  hid = 1;
	if (H < 50)  hid = 0;

	id = hid;

	return id;
}

//钙
int Calcium(int R, int G, int B)
{
	int id = 0;

	float H = 0, S = 0, V = 0;
	RGB_HSV(R, G, B, H, S, V);

	int rid = 0, gid = 0, bid = 0, hid = 0, sid = 0, vid = 0;

	return id;
}

int Algorithm(int item, unsigned char R, unsigned char G, unsigned char B)
{
	switch (item)
	{
	case 0:
		return Leukocytes(R, G, B);
		break;
	case 1:
		return Nitrite(R, G, B);
		break;
	case 2:
		return UrinaryBladder(R, G, B);
		break;
	case 3:
		return Protein(R, G, B);
		break;
	case 4:
		return PH(R, G, B);
		break;
	case 5:
		return OccultBlood(R, G, B);
		break;
	case 6:
		return Proportion(R, G, B);
		break;
	case 7:
		return Ketone(R, G, B);
		break;
	case 8:
		return Bilirubin(R, G, B);
		break;
	case 9:
		return Glucose(R, G, B);
		break;
	case 10:
		return AscorbicAcid(R, G, B);
		break;
	case 11:
		return MicroAlbumin(R, G, B);
		break;
	case 12:
		return Creatinine(R, G, B);
		break;
	case 13:
		return Calcium(R, G, B);
		break;

	default:
		break;
	}
}